#include <gio/gdesktopappinfo.h>
#include <gio/gio.h>
#include <glib.h>
#include <glib/gstdio.h>
#include <stdlib.h>
#include <string.h>

#include "nautilus_functions.h"


/*
 * nautilus_link_get_link_icon_from_desktop:
 *
 * copied from nautilus: fm-desktop-icon-view.c
 */
static char * nautilus_link_get_link_icon_from_desktop (GKeyFile *key_file)
{
	char *icon_uri, *icon, *p, *type;

	icon_uri = g_key_file_get_string (key_file, MAIN_GROUP, "X-Nautilus-Icon", NULL);
	if (icon_uri != NULL) {
		return icon_uri;
	}

	icon = g_key_file_get_string (key_file, MAIN_GROUP, "Icon", NULL);
	if (icon != NULL) {
		if (!g_path_is_absolute (icon)) {
			/* Strip out any extension on non-filename icons. Old desktop files may have this */
			p = strchr (icon, '.');
                        /* Only strip known icon extensions */
			if ((p != NULL) &&
			    ((g_ascii_strcasecmp (p, ".png") == 0)
			     || (g_ascii_strcasecmp (p, ".svn") == 0)
			     || (g_ascii_strcasecmp (p, ".jpg") == 0)
			     || (g_ascii_strcasecmp (p, ".xpm") == 0)
			     || (g_ascii_strcasecmp (p, ".bmp") == 0)
			     || (g_ascii_strcasecmp (p, ".jpeg") == 0))) {
				*p = 0;
			}
		}
		return icon;
	}

	type = g_key_file_get_string (key_file, MAIN_GROUP, "Type", NULL);
	if (g_strcmp0 (type, "Application") == 0) {
		icon = g_strdup ("gnome-fs-executable");
	} else if (g_strcmp0 (type, "Link") == 0) {
		icon = g_strdup ("gnome-dev-symlink");
	} else if (g_strcmp0 (type, "FSDevice") == 0) {
		icon = g_strdup ("gnome-dev-harddisk");
	} else if (g_strcmp0 (type, "Directory") == 0) {
		icon = g_strdup (NAUTILUS_ICON_FOLDER);
	} else if (g_strcmp0 (type, "Service") == 0 ||
		   g_strcmp0 (type, "ServiceType") == 0) {
		icon = g_strdup ("gnome-fs-web");
	} else {
		icon = g_strdup ("gnome-fs-regular");
	}
	g_free (type);

	return icon;
}


